package ThreadProjectTest.sync;

/*
 * Author: JacksonCoder
 * FileName: sychronizedTest
 * Version: 1.0.0
 */
public class synchronizedTest {

    public static void main(String[] args) {

        BankAccount accountA = new BankAccount(10000);
        BankAccount accountB = new BankAccount(1000);

        // 创建2个线程进行转账
        Thread thread1 = new Thread(()->accountA.transfer(accountB,200));
        Thread thread2 = new Thread(()->accountB.transfer(accountA,600));

        thread1.start();
        thread2.start();

        // 等待线程完成
        try{
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // terminate   print the final balance
        System.out.println("Final balance of accountA: " + accountA.getBalance());
        System.out.println("Final balance of accountB: " + accountB.getBalance());
    }

}
